var ChatManager = {
	addTab: function(area, tab, label, jid) {
		$(area).tabs('add', tab, label);
		$(tab).append(
			  "<div class='chat-messages'></div>"
			+ "<input type='text' class='chat-input'>"
		);
		
		ChatManager.setJid(tab, jid);
		ChatManager.selectTab(area, tab);
	},
	
	selectTab: function(area, tab) {
		$(area).tabs('select', tab);
		$(tab).find('input').focus();
	},
	
	setJid: function(tab, jid) {
		$(tab).data('jid', jid);
	},
	
	getJid: function(tab) {
		return $(tab).data('jid');
	},
	
	setComposing: function(tab, composing) {
		$(tab).data('composing', composing);
	},
	
	getComposing: function(tab) {
		return $(tab).data('composing');
	},
	
	showMessage: function(tab, name, body, id, me) {
		var meClass = '';
		if (me) {
			meClass = ' me';
		}
		
		$(tab).find('.chat-messages').append(
			  "<div class='chat-message'>"
			+ "&lt;<span class='chat-name" + meClass + "'>" + name + "</span>&gt;"
			+ "<span class='chat-text'>" + body + "</span>"
			+ "</div>"
		);
		
		ChatManager.scrollChat(tab);
	},
	
	showComposing: function(tab, name) {
		if ($(tab).find('.chat-event').length > 0) return;
		
		$(tab).find('.chat-messages').append(
			  "<div class='chat-event'>" + name + " is typing...</div>"
		);
		
		ChatManager.scrollChat(tab);
	},
	
	removeComposing: function(tab) {
		$(tab).find('.chat-event').remove();
	},
	
	scrollChat: function(tab) {
		var div = $(tab).find('.chat-messages').get(0);
		div.scrollTop = div.scrollHeight;
	}
};